package com.ruiyian.project.plan.service.impl;

import com.ruiyian.project.log.domain.PlanLogMain;
import com.ruiyian.project.log.service.PlanLogMainService;
import com.ruiyian.project.plan.domain.Plan;
import com.ruiyian.project.plan.mapper.PlanMapper;
import com.ruiyian.project.plan.service.PlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/1/29
 * Desc: 预案实现
 */
@Slf4j
@Service
public class PlanServiceImpl implements PlanService {
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private PlanLogMainService planLogMainService;
    /**
     * 列表查询
     * @param plan
     * @return
     */
    @Override
    public List<Plan> selectList(Plan plan) {
        return planMapper.selectList(plan);
    }

    /**
     * 保存
     * @param plan
     */
    @Override
    public void save(Plan plan) {
        planMapper.save(plan);
    }

    /**
     * 编辑
     * @param plan
     */
    @Override
    public void edit(Plan plan) {
        planMapper.edit(plan);
    }

    /**
     * 根据id获取对象
     * @param id
     * @return
     */
    @Override
    public Plan findOne(Long id) {
        return planMapper.findOne(id);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        planMapper.delete(id);
    }

    /**
     * 批量删除
     * @param list
     */
    @Override
    public void deleteBatch(List<Plan> list) {
        planMapper.deleteBatch(list);
    }


    /**
     * 生成日志
     */
    @Override
    public void createLog(Long id) {
        Plan plan = this.findOne(id);
        PlanLogMain planLogMain = new PlanLogMain();
        try {
            planLogMain.setName(plan.getName());
            planLogMain.setPlanId(plan.getId());
            planLogMain.setType("1"); //1 预案 0,操作记录
            planLogMain.setProvince(plan.getProvince());
            planLogMain.setArea(plan.getArea());
            planLogMain.setBranch(plan.getBranch());
            planLogMain.setTunnelId(plan.getTunnelId());
            planLogMain.setTunnelName(plan.getTunnelName());
            planLogMain.setHoleName(plan.getHoleName());
            planLogMain.setDirection(plan.getDirection());
            planLogMain.setTunnelHoleId(plan.getHoleId());
            planLogMain.setConditions(plan.getName());
            planLogMain.setParamValue(plan.getCode());
//            planLogMain.setCreateBy(SecurityUtils.getUsername());
            planLogMain.setCreateTime(new Date());
            planLogMain.setMethod("2");
            planLogMain.setSource("网页操作");
            planLogMainService.save(planLogMain);
        } catch (Exception e) {
            log.error("生成预案日志失败"+e.getMessage());
        }
    }

    /**
     * 查询预案 根据隧道id和预案编码
     * @param plan
     * @return
     */
    @Override
    public Plan getPlanInfo(Plan plan) {
        return planMapper.getPlanInfo(plan);
    }

    @Override
    public Plan getPlanInfoByTunnel(Long tunnelId) {
        return planMapper.getPlanInfoByTunnel(tunnelId);
    }
}
